<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 18/4/4
 * Time: 15:49
 */

Yaf_Loader::import(CONTROLLERS_PATH.'Controller.php');

class BackdropController extends Controller
{
    public function init()
    {
        $this->authentication = $this->authentication();
        if(!$this->authentication)
        {
            $this->redirect('/');
            exit;
        }
        $this->backdrop = new BackdropModel();
        $this->getView()->assign("user", $this->authentication['data']);
        $this->group = new GroupModel();
        $group = $this->group->listsAction($this->authentication['data']);
        $this->getView()->assign("group", $group);
    }
    /**
     * 图片列表
     */
    public function listsAction()
    {
        $content = $this->getRequest()->getPost();
        $validate = new Validate([
            'token' => 'require',
        ]);
        $validate->message([
            'token.require' => 'token不能为空',
        ]);
        if ($validate->check($content)) {
            $data = $this->backdrop->listsAction($this->authentication['data']);
            $code = 200;  $data['message'] = '您的账户已在其他设备上登录';
        }else{
            $code = 400;
            $data['message'] = $validate->getError();
        }
        $this->returnApi($data,$code,$content);
    }

    /**
     * 图片添加
     */
    public function addAction()
    {
        $content = $this->getRequest()->getPost();
        $validate = new Validate([
            'status' => 'require|number|between:1,2',
        ]);
        $validate->message([
            'status.require' => '图片状态不能为空',
            'status.number' => '图片状态类型错误',
            'status.between' => '图片状态不在范围内',
        ]);
        if ($validate->check($content)) {
            $data = $this->backdrop->addAction($content,$this->authentication['data']);
            $code = 200;
        }else{
            $code = 400;
            $data['message'] = $validate->getError();
        }
        $this->returnApi($data,$code,$content);
    }

    /**
     * 图片修改
     */
    public function editAction()
    {
        $content = $this->getRequest()->getPost();
        $validate = new Validate([
            'bid' => 'require|number|egt:1|exist:backdrop,id',
            'status' => 'require|number|between:1,2',
        ]);
        $validate->message([
            'bid.require' => '图片ID不能为空',
            'bid.number' => '图片ID只能是数字',
            'bid.egt' => '图片ID错误',
            'bid.exist' => '图片ID不存在',
            'status.require' => '图片状态不能为空',
            'status.number' => '图片状态类型错误',
            'status.between' => '图片状态不在范围内',
        ]);
        if ($validate->check($content)) {
            $data = $this->backdrop->editAction($content);
            $code = 200;
            if(!empty($data['code'])){
                $code = $data['code'];
                unset($data['code']);
            }
        }else{
            $code = 400;
            $data['message'] = $validate->getError();
        }
        $this->returnApi($data,$code,$content);
    }
}